package com.hdoit.kylin.web.baseInfo.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.baseInfo.model.School;
import com.hdoit.kylin.logical.baseInfo.service.ISchoolService;
import com.hdoit.kylin.logical.viewmodel.baseInfo.SchoolSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

@Controller
@RequestMapping(value="/admin/baseInfo/school/")
public class SchoolController extends BaseController{
	@Override
	public String getRoutePath() {
		return "areas/admin/baseInfo/school/";
	}
	
	private ISchoolService schoolService;
	
	public ISchoolService getSchoolService() {
		return schoolService;
	}
	
	@Resource
	public void setSchoolService(ISchoolService schoolService) {
		this.schoolService = schoolService;
	}
	
	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "schoolDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";
	
	@RequestMapping(value=indexViewName, method = { RequestMethod.GET, RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel, SchoolSearch search, boolean ajaxPost) {
		pagerModel.setTotalAmount(schoolService.queryCount(search));
		List<School> schools = schoolService.queryList(search, pagerModel);
		
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("schools", schools);
		model.addAttribute("schoolSearch", search);
		
		if(ajaxPost) {
			return view(outputViewName);
		}
		
		return view(indexViewName);
	}
	
	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String academyDetailed(Model model, String key) {
		School school = new School();
		if(!StringUtils.isEmpty(key)) {
			school = schoolService.queryModel(key);
		}
		
		model.addAttribute("school", school);
		
		return view(detailedViewName);
	}
	
	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model, School school) {
		if (StringUtils.isEmpty(school.getSchoolId())) {
			school.setSchoolId(CommonUtils.getKeyId());
			school.setModelStatus(ModelStatus.add);
		} else {
			school.setModelStatus(ModelStatus.update);
		}
		
		ActionResult actionResult;
		if (schoolService.applyChange(school)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}
		model.addAttribute("school", school);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		return view(detailedViewName);
	}

	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (schoolService.deleteModel(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}
